package com.yinhe.system.service.impl;

import com.yinhe.common.annotation.DataScope;
import com.yinhe.common.exception.ServiceException;
import com.yinhe.common.utils.DateUtils;
import com.yinhe.common.utils.StringUtils;
import com.yinhe.common.utils.bean.BeanValidators;
import com.yinhe.system.domain.YhContract;
import com.yinhe.system.mapper.YhContractMapper;
import com.yinhe.system.service.IYhContractService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.validation.Validator;
import java.util.List;

/**
 * 合同台账Service业务层处理
 * 
 * @author yinhe
 * @date 2022-07-14
 */
@Service
public class YhContractServiceImpl implements IYhContractService 
{
    @Autowired
    private YhContractMapper yhContractMapper;
    @Autowired
    protected Validator validator;
    /**
     * 查询合同台账
     * 
     * @param id 合同台账主键
     * @return 合同台账
     */
    @Override
    public YhContract selectYhContractById(Long id)
    {
        return yhContractMapper.selectYhContractById(id);
    }

    /**
     * 通过用户名查询用户
     *
     * @param userName 用户名
     * @return 用户对象信息
     */

    /**
     * 查询合同台账列表
     * 
     * @param yhContract 合同台账
     * @return 合同台账
     */
    @Override
    @DataScope(deptAlias = "c", userAlias = "c")
    public List<YhContract> selectYhContractList(YhContract yhContract)
    {
        return yhContractMapper.selectYhContractList(yhContract);
    }

    /**
     * 查询合同台账列表,用于资料查询
     *
     * @param yhContract 合同台账
     * @return 合同台账
     */
    @Override
    public List<YhContract> selectYhContractSearchList(YhContract yhContract)
    {
        return yhContractMapper.selectYhContractSearchList(yhContract);
    }

    /**
     * 通过流水号，查询装备列表
     *
     * @param sequenceNumber 流水号
     * @return 合同台账
     */
    @Override
    public List<YhContract> selectYhContractSequenceNumber(String sequenceNumber) {
        return yhContractMapper.selectYhContractSequenceNumber(sequenceNumber);
    }

    /**
     * 新增合同台账
     * 
     * @param yhContract 合同台账
     * @return 结果
     */
    @Override
    public int insertYhContract(YhContract yhContract)
    {
        yhContract.setCreateTime(DateUtils.getNowDate());
        return yhContractMapper.insertYhContract(yhContract);
    }

    /**
     * 修改合同台账
     * 
     * @param yhContract 合同台账
     * @return 结果
     */
    @Override
    public int updateYhContract(YhContract yhContract)
    {
        yhContract.setUpdateTime(DateUtils.getNowDate());
        return yhContractMapper.updateYhContract(yhContract);
    }

    /**
     * 批量删除合同台账
     * 
     * @param ids 需要删除的合同台账主键
     * @return 结果
     */
    @Override
    public int deleteYhContractByIds(Long[] ids)
    {
        return yhContractMapper.deleteYhContractByIds(ids);
    }

    /**
     * 删除合同台账信息
     * 
     * @param id 合同台账主键
     * @return 结果
     */
    @Override
    public int deleteYhContractById(Long id)
    {
        return yhContractMapper.deleteYhContractById(id);
    }
    /**
     * 导入合同台账数据
     *
     * @param userList 用户数据列表
     * @param isUpdateSupport 是否更新支持，如果已存在，则进行更新数据
     * @param operName 操作用户
     * @return 结果
     */
    @Override
    public String importContract(List<YhContract> contractList, Boolean isUpdateSupport)
    {
        for (YhContract a:contractList) {
            a.setContractAssociationStatus("1");
            a.setContractAssociationNo(a.getSequenceNumber() + a.getQuantity()+a.getEquipVersion());
            long represent = Long.parseLong(a.getRepresentativeOffice());
            a.setDeptId(represent);
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (YhContract contract : contractList) {
            try {
                YhContract c = yhContractMapper.selectYhContractAssociationNo(contract.getContractAssociationNo());
                if (StringUtils.isNull(c)) {
                    BeanValidators.validateWithException(validator, contract);
                    this.insertYhContract(contract);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 导入成功");

                } else if (isUpdateSupport) {
                    BeanValidators.validateWithException(validator, contract);
                    String number = contract.getContractAssociationNo();
                    YhContract result =   yhContractMapper.selectYhContractAssociationNo(number);
                    Long id = result.getId();
                    contract.setId(id);
                    this.updateYhContract(contract);
                    successNum++;
                    successMsg.append("<br/>" + successNum + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + " 已存在");
                }
            }
            catch (Exception e)
            {
                failureNum++;
                String msg = "<br/>" + failureNum + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
            }
        }
        if (failureNum > 0)
        {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        else
        {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }

        return successMsg.toString();
    }
}